package models

import (
	"fmt"
	"github.com/astaxie/beego/orm"
)

type Article struct {
	Id          int64  `json:"id";orm:"column(id);pk;auto"`
	Title       string `json:"title"`
	Author      string `json:"author"`
	Source      string `json:"source"`
	Link        string `json:"link"`
	CrateTime   int64  `json:"crateTime"`
	Description string `json:"description"`
}

func ArticleInsertOrUpdate(art Article) (int64) {
	o := orm.NewOrm()
	//"colu=colu+value")
	// mysql:InsertOrUpdate(model) or InsertOrUpdate(model,"colu=colu+value")
	// if colu type is integer : can use(+-*/), string : convert(colu,"value")
	s:="title='"+art.Title+"'"
	l:="link='"+art.Link+"'"
	fmt.Println("colu;",s,l)
	id, e := o.InsertOrUpdate(&art, "title","link")
	if e == nil {
		return id
	}
	fmt.Println(e)
	return -1
}

func ArticleGet(page int, size int) ([]Article, int64) {
	o := orm.NewOrm()
	list := []Article{}
	_, err := o.QueryTable("article").OrderBy("-crateTime").Limit(size, (page-1)*size).All(&list)
	if err != nil {
		fmt.Println("quert err ", err)
		return nil, 0
	}
	TotalCount, _ := o.QueryTable("article").Count()
	fmt.Println("page ", page, " size-》 ", len(list), " count ", TotalCount)
	return list, TotalCount
	//o.QueryTable("user").Limit(pageSize,(ps-1)*pageSize).All(user)
	//
	//2.栗子：
	//
	//①select * from mytbl limit 10000,100
	//
	//	上边SQL语句表示从表mytbl中拿数据，跳过10000行之后，拿100行
	//
	//	②select * from mytbl limit 0,100
	//
	//		表示从表mytbl拿数据，跳过0行之后，拿取100行
}
